package com.ruoyi.contact.controller;

import java.util.List;


import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.contact.domain.ContactCategory;
import com.ruoyi.contact.domain.ContactLevel;
import com.ruoyi.contact.service.IContactCategoryService;
import com.ruoyi.contact.service.IContactLevelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contact.domain.ContactInfo;
import com.ruoyi.contact.service.IContactInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 联系人信息Controller
 *
 * @author ruoyi
 * @date 2025-05-30
 */
@Controller
@RequestMapping("/contact/info")
public class ContactInfoController extends BaseController
{
    private String prefix = "contact/info";

    @Autowired
    private IContactInfoService contactInfoService;
    @Autowired
    private IContactCategoryService contactCategoryService;
    @Autowired
    private IContactLevelService contactLevelService;

    @RequiresPermissions("contact:info:view")
    @GetMapping()
    public String info(ModelMap mmap)
    {
        ContactCategory contactCategory = new ContactCategory();
        contactCategory.setUserId(ShiroUtils.getUserId());
        mmap.put("categories",contactCategoryService.selectContactCategoryList(null));

        ContactLevel contactLevel = new ContactLevel();
        contactLevel.setUserId(ShiroUtils.getUserId());
        mmap.put("levels",contactLevelService.selectContactLevelList(null));
        return prefix + "/info";
    }

    /**
     * 查询联系人信息列表
     */
    @RequiresPermissions("contact:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ContactInfo contactInfo)
    {
        startPage();
        List<ContactInfo> list = contactInfoService.selectContactInfoList(contactInfo);
        return getDataTable(list);
    }

    /**
     * 导出联系人信息列表
     */
    @RequiresPermissions("contact:info:export")
    @Log(title = "联系人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ContactInfo contactInfo)
    {
        List<ContactInfo> list = contactInfoService.selectContactInfoList(contactInfo);
        ExcelUtil<ContactInfo> util = new ExcelUtil<ContactInfo>(ContactInfo.class);
        return util.exportExcel(list, "联系人信息数据");
    }

    /**
     * 新增联系人信息
     */
    @RequiresPermissions("contact:info:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());

        ContactCategory contactCategory = new ContactCategory();
        contactCategory.setUserId(ShiroUtils.getUserId());
        mmap.put("categories",contactCategoryService.selectContactCategoryList(null));
        ContactLevel contactLevel = new ContactLevel();
        contactLevel.setUserId(ShiroUtils.getUserId());
        mmap.put("levels",contactLevelService.selectContactLevelList(null));

        //之前添加 意义不明确 不敢删除
        mmap.put("categoryList",contactCategoryService.selectContactCategoryList(null));
        mmap.put("levelList", contactLevelService.selectContactLevelList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存联系人信息
     */
    @RequiresPermissions("contact:info:add")
    @Log(title = "联系人信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ContactInfo contactInfo)
    {
        contactInfo.setUserId(ShiroUtils.getUserId());
        return toAjax(contactInfoService.insertContactInfo(contactInfo));
    }

    /**
     * 修改联系人信息
     */
    @RequiresPermissions("contact:info:edit")
    @GetMapping("/edit/{contactId}")
    public String edit(@PathVariable("contactId") Long contactId, ModelMap mmap)
    {
        ContactInfo contactInfo = contactInfoService.selectContactInfoByContactId(contactId);
        mmap.put("categoryList",contactCategoryService.selectContactCategoryList(null));
        ContactCategory contactCategory = new ContactCategory();
        contactCategory.setUserId(ShiroUtils.getUserId());
        mmap.put("levelList", contactLevelService.selectContactLevelList(null));
        ContactLevel contactLevel = new ContactLevel();
        contactLevel.setUserId(ShiroUtils.getUserId());
        mmap.put("contactInfo", contactInfo);
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/edit";
    }

    /**
     * 修改保存联系人信息
     */
    @RequiresPermissions("contact:info:edit")
    @Log(title = "联系人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ContactInfo contactInfo)
    {
        contactInfo.setUserId(ShiroUtils.getUserId());
        return toAjax(contactInfoService.updateContactInfo(contactInfo));
    }

    /**
     * 删除联系人信息
     */
    @RequiresPermissions("contact:info:remove")
    @Log(title = "联系人信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(contactInfoService.deleteContactInfoByContactIds(ids));
    }
}
